package com.demo

import javax.swing.{JOptionPane, JTextField, JPasswordField}
import scala.swing._

/** Twitter LoginDialog
 *  Instantiating this class will prompt the user to enter
 *  their Twitter credentials. The user will be reprompted
 *  until valid credentials are entered
 *
 *  scala-swing does not yet support dialog boxes, so we're
 *  dropping down to Plain Old Java Swing
 *
 *  @author Jorge Ortiz
 *  @version 0.1
 */
class LoginDialog {
  val Username = new JTextField
  val Password = new JPasswordField
  val arr: Array[java.lang.Object] =
    List("Name", Username, "Password", Password).toArray
  val options: Array[java.lang.Object] =
    Array("Login", "Quit")
  
  def promptLogin = {
    if (JOptionPane.showOptionDialog(
      null,
      arr,
      "Login",
      JOptionPane.OK_CANCEL_OPTION,
      JOptionPane.QUESTION_MESSAGE,
      null,
      options,
      null) == options.indexOf("Quit")) {
        // Quit if the user clicks "Quit"
        System.exit(0)
      }
  }

  def valid =
    Twitter.verifyCredentials(Username.getText, Password.getText)
  
  promptLogin
  while(!valid) {
    promptLogin
  }
}
